package com.cbwleft.streaming.sse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Repository;

@Repository
public class PositionRepository {

    private double[][] positions = {
            {112.8727457306042, 28.236109760002627},
            {112.87264684374745, 28.23609088834072},
            {112.87269978114846, 28.236075819247393},
            {112.87270777338628, 28.236094809122264},
            {112.87272275539394, 28.236086789533346},
            {112.87271476417844, 28.236080799836557},
            {112.87272275452557, 28.23607578937977},
            {112.87270776991276, 28.23605080850836},
            {112.87216641678184, 28.236236519245363},
            {112.8714962134976, 28.236246407727876},
            {112.86950672913198, 28.237112131849837},
            {112.8674115135142, 28.238066118173748},
            {112.86655868393193, 28.238453366650372},
            {112.86658864258496, 28.23844132250412},
            {112.86659663165827, 28.238439310755425},
            {112.86661161147192, 28.238439288792836},
            {112.86661161194601, 28.23844528889188},
            {112.86657366388737, 28.23845534470655},
            {112.86495895221505, 28.239395761732588},
            {112.86358695444298, 28.24021586594903},
            {112.86131738838692, 28.2417514464558},
            {112.85916175735022, 28.243089949485075},
            {112.85750837206872, 28.243595688967467},
            {112.85537578712065, 28.243704285450622},
            {112.85357773194323, 28.243746377341544},
            {112.85159005671278, 28.243815854794978},
            {112.84951859073084, 28.243896538647682},
            {112.84772869449208, 28.24396276547714},
            {112.84590090743967, 28.244040097842614},
            {112.84435465423198, 28.244092942309667},
            {112.84303002581473, 28.24413139528425},
            {112.8413011452491, 28.244197616733633},
            {112.83954932823005, 28.24426689962248},
            {112.83970902518416, 28.244108596210946},
            {112.84175731072533, 28.244064762176887},
            {112.84309788436056, 28.2438832637047},
            {112.84286217707754, 28.242244665890254},
            {112.84277822934996, 28.2410057970065},
            {112.84441529377447, 28.240916763606034},
            {112.84637282206012, 28.240848167669103},
            {112.8471494584993, 28.240842751859127},
            {112.84756077121251, 28.241241012431946},
            {112.84781944785594, 28.24284857649836},
            {112.84833063733888, 28.24383367365475},
            {112.85051587108154, 28.243731755101532},
            {112.85283196277518, 28.24366367267571},
            {112.8545680916778, 28.243587664747295},
            {112.85665571646378, 28.243515114374883},
            {112.85854472301655, 28.24323996477973},
            {112.86024304917785, 28.24236117886001},
            {112.86207122802823, 28.241174242906688},
            {112.86350307712914, 28.2402489957255},
            {112.86469932216603, 28.23948715531645},
            {112.86597149424989, 28.238623235364035},
            {112.86645181961644, 28.238344521787667},
            {112.86818146674888, 28.23761100554733},
            {112.87012393330443, 28.23681727454161},
            {112.87186176266293, 28.236057918941032},
            {112.87205252912122, 28.23595966539507},
            {112.87305832212562, 28.235499346949922},
            {112.8733090386847, 28.235539025003877},
            {112.87289055064608, 28.235949569933073},
            {112.87284460562745, 28.235974629804673}
    };

    public double[] getPosition() {
        return positions[i];
    }

    private int i;

    @Autowired
    private ApplicationEventPublisher publisher;

    @Scheduled(fixedDelay = 1 * 1000)
    public void trigger() {
        i = i % positions.length;
        publisher.publishEvent(new PositionEvent(this, positions[i]));
        i++;
    }

}
